package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.stream.Collectors;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.AgendaMapper;
import com.ruoyi.system.domain.Agenda;
import com.ruoyi.system.service.IAgendaService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 会议议程Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-13
 */
@Service
public class AgendaServiceImpl implements IAgendaService 
{
    @Autowired
    private AgendaMapper agendaMapper;

    /**
     * 查询会议议程
     * 
     * @param agendaId 会议议程主键
     * @return 会议议程
     */
    @Override
    public Agenda selectAgendaByAgendaId(Long agendaId)
    {
        return agendaMapper.selectAgendaByAgendaId(agendaId);
    }

    /**
     * 查询会议议程列表
     * 
     * @param agenda 会议议程
     * @return 会议议程
     */
    @Override
    public List<Agenda> selectAgendaList(Agenda agenda)
    {
        return agendaMapper.selectAgendaList(agenda);
    }

    /**
     * 新增会议议程
     * 
     * @param agenda 会议议程
     * @return 结果
     */
    @Override
    public int insertAgenda(Agenda agenda)
    {
        agenda.setCreateTime(DateUtils.getNowDate());
        return agendaMapper.insertAgenda(agenda);
    }

    /**
     * 修改会议议程
     * 
     * @param agenda 会议议程
     * @return 结果
     */
    @Override
    public int updateAgenda(Agenda agenda)
    {
        agenda.setUpdateTime(DateUtils.getNowDate());
        return agendaMapper.updateAgenda(agenda);
    }

    /**
     * 批量删除会议议程
     * 
     * @param agendaIds 需要删除的会议议程主键
     * @return 结果
     */
    @Override
    public int deleteAgendaByAgendaIds(Long[] agendaIds)
    {
        return agendaMapper.deleteAgendaByAgendaIds(agendaIds);
    }

    /**
     * 删除会议议程信息
     * 
     * @param agendaId 会议议程主键
     * @return 结果
     */
    @Override
    public int deleteAgendaByAgendaId(Long agendaId)
    {
        return agendaMapper.deleteAgendaByAgendaId(agendaId);
    }

    /**
     * 根据会议ID查询议程列表
     *
     * @param meetId 会议ID
     * @return 议程列表
     */
    @Override
    public List<Agenda> selectAgendaByMeetId(Long meetId) {
        return agendaMapper.selectAgendaByMeetId(meetId);
    }

    /**
     * 批量新增会议议程
     * 
     * @param agendas 议程列表
     * @return 结果
     */
    @Override
    @Transactional
    public int batchInsertAgenda(List<Agenda> agendas) {
        // 先删除该会议的所有议程
        if (!agendas.isEmpty()) {
            Long meetId = agendas.get(0).getMeetId();
            agendaMapper.deleteAgendaByMeetId(meetId);
        }
        // 批量插入新的议程
        return agendaMapper.batchInsertAgenda(agendas);
    }

    /**
     * 批量修改会议议程
     * 
     * @param agendas 议程列表
     * @return 结果
     */
    @Override
    @Transactional
    public int batchUpdateAgenda(List<Agenda> agendas) {
        if (agendas.isEmpty()) {
            return 0;
        }
        
        Long meetId = agendas.get(0).getMeetId();
        
        // 获取现有的议程ID列表
        List<Long> existingIds = agendas.stream()
            .map(Agenda::getAgendaId)
            .filter(id -> id != null)
            .collect(Collectors.toList());
        
        // 删除不在更新列表中的议程
        if (!existingIds.isEmpty()) {
            agendaMapper.deleteOtherAgendas(meetId, existingIds);
        }
        
        // 更新或插入议程
        for (Agenda agenda : agendas) {
            if (agenda.getAgendaId() != null) {
                agenda.setUpdateTime(DateUtils.getNowDate());
                agendaMapper.updateAgenda(agenda);
            } else {
                agenda.setCreateTime(DateUtils.getNowDate());
                agendaMapper.insertAgenda(agenda);
            }
        }
        
        return agendas.size();
    }
}
